package com.jinyi.wwdeng.ui.main.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.status.StatusRepository
import com.jinyi.wwdeng.ui.main.contract.MainContract
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:21
 * @Annotation：Presenter 实现类
 */
class MainPresenterImpl: BasePresenter<MainContract.View,MainModelImpl>(),MainContract.Presenter {

    override fun createModel(): MainModelImpl {
        return MainModelImpl(this)
    }

    /**
     * 获取用户流程状态
     */
    fun loadProcessRequest(){
        if (isViewDestory()) {
            return
        }
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_PROCESS)
        }
        model!!.loadProcessRequest(params)
    }

    /**
     * 流程状态获取成功
     */
    override fun onProcessSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.hideLoading()
        when(data.optInt("code")){
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                if(obj != null){
                    // TODO: 测试添加 Step 类型
//                    val allStepTitles = obj.optJSONArray("allStepTitles")
//                    allStepTitles.put("Ocr Info")
//                    allStepTitles.put("Face recognition")
//                    val allSteps = obj.optJSONArray("allSteps")
//                    allSteps.put("ocr_info")
//                    allSteps.put("face_recognition")
//                    val steps = obj.optJSONArray("steps")
//                    steps.put("ocr_info")
//                    steps.put("face_recognition")

//                    val steps = JSONArray()
//                    steps.put("account_info")
//                    obj.put("steps",steps)


                    //保存用户流程状态
                    AppConfigs.saveSPData(AppConfigs.SP_KEY.SP_HENECREDIT_PROCESS,obj.toString())
                    mvpView!!.updateProcessSuccess()
                }

                //1、判断是否存在 订单状态
                if (obj != null && obj.has("orderStatus")){
                    val orderStatus = obj.optString("orderStatus")
                    //待还款
                    if (StatusRepository(null,orderStatus).isRepayNow()){
                        mvpView!!.showCreditRepaymentPage()
                        return
                    }
                    //放款中
                    if (StatusRepository(null,orderStatus).isDisbursing()){
                        mvpView!!.showCreditAuditPage(obj)
                        return
                    }
                    //申请贷款被拒绝
                    if (StatusRepository(null,orderStatus).isRejected()){
                        mvpView!!.showCreditApplyFailedPage(obj)
                        return
                    }
                }

                //2、判断是否存在 贷款申请状态
                if (obj != null && obj.has("applyStatus")){
                    val applyStatus = obj.optString("applyStatus")
                    //审核中
                    if (StatusRepository(applyStatus).isReviewing()){
                        mvpView!!.showCreditAuditPage(obj)
                        return
                    }
                    //被拒绝
                    if (StatusRepository(applyStatus).isRejected()){
                        mvpView!!.showCreditApplyFailedPage(obj)
                        return
                    }
                }

                //3、直接显示贷款额度页面
                if (obj != null){
                    mvpView!!.showCreditLimitPage(obj.optJSONArray("steps")!!.length() == 0)
                }
            }
            else ->{//失败
                val msg = data.optString("msg")
                mvpView!!.showToast(msg)
            }
        }
    }

    /**
     * 接口请求出错
     */
    override fun onRequestError(errorMsg: String) {
        mvpView!!.showToast(errorMsg)
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}